package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.CustomerReservationDto;
import com.zzyl.entity.CountByTimeMethod;
import com.zzyl.entity.CustomerReservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CustomerReservationVo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    //查询取消预约数量
    @Override
    public Integer queryDeclineReserNum() {
        Long userId = UserThreadLocal.getUserId();
        return customerReservationMapper.queryDeclineReserNum(userId);
    }

    //查询每个时间段剩余预约次数
    @Override
    public List<CustomerReservationVo> queryRestCountOnEveryTime(Long time) {
        LocalDate localDate = Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        List<CustomerReservationVo> customerReservationVos = customerReservationMapper
                .queryRestCountOnEveryTime(localDate);
        CountByTimeMethod countByTimeMethod = new CountByTimeMethod();
        return countByTimeMethod.count(customerReservationVos, localDate);
    }

    // 新增预约
    @Override
    public void addReservation(CustomerReservationDto customerReservationDto) {
        CustomerReservation bean = BeanUtil.toBean(customerReservationDto, CustomerReservation.class);
        bean.setStatus(0);
        bean.setType(0);
        customerReservationMapper.addReservation(bean);
    }

    //分页查询预约
    @Override
    public PageResponse<CustomerReservationVo> queryPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = UserThreadLocal.getUserId();
        Page<CustomerReservationVo> page = customerReservationMapper.queryPage(status, userId);
        return PageResponse.of(page, CustomerReservationVo.class);
    }

    //取消预约
    @Override
    public void cancelReservation(Long id) {
        Long userId = UserThreadLocal.getUserId();
        customerReservationMapper.cancelReservation(id, userId);
    }

    //预约过期
    @Override
    public void expireReservation(LocalDateTime now) {
        customerReservationMapper.expireReservation(now);
    }

//    @0verride
    //public int queryCancalCount(Long userId, LocalDate time){
    //LocalDateTime statTime =time.atTime( 0, 0, 0);
    //LocalDateTime endTime = time.atTime( 23, 59, 59);
    //int count=reservationMapper.queryCancalCount(userId,statTime,endTime);
    //return count;
    //}
//@0verride new
//public void insert(VisitDto visitDto){
    //Visit visit = new Visit();
    //BeanUtil.copyProperties(visitDto,visit);
    //visit.setType(0); //参观预约
    //visit.setStatus(0); //待报道
    ////查询取消预约是否大于三次
    //Long userId =UserThreadLocal.getUserId();
    //LocalDate time = LocalDate.now();
    // LocalDateTime statTime = time.atTime( hour: 0,minute: 0,second: 0);
    // LocalDateTime endTime = time.atTime( hour: 23,minute:59,second:59);
    // int count = reservationMapper.queryCancalCount(userId,statTime,endTime)
    //if(count >=3){
    //  throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
   //}
    //判断时间段9:30这个时间段不能大于6次
    //reservationMapper.insert(visit);
}


















